//全局路径
// const commonUrl = 'http://172.17.3.210:8080'
//解析json
function parseJSON(response) {
    return response.json()
}
//检查请求状态
function checkStatus(response) {
    if (response.status >= 200 && response.status < 500) {
        return response
    }
    const error = new Error(response.statusText)
    error.response = response
    throw error
}

//修改get的参数
function getMth(obj) {
    console.log(obj)
    let temp = ""
    for (let i in obj) {
        temp += i + "=" + obj[i] + "&"
    }
    return temp
}

export default function Fetch(options = {}) {
    let { data, url, body } = options
    console.log(data)
    options = { ...options }
    options.mode = "cors"//跨域
    delete options.url
    if (data) {
        delete options.data
        options.body = JSON.stringify(data)
    }
    if (options.method == "get") {
        let result = getMth(body)
        result = result.slice(0, result.length - 1)
        delete options.body
        console.log(result)
        if(result!=" "){
            url += "?" + result
            console.log(url)
        }
       
    }
    options.headers = {
        "Content-Type": "application/json;charset=UTF-8"
    }
    console.log(options)

    return fetch(url, options, { credentials: 'include' })
        .then(checkStatus)

         .then(parseJSON)
        .catch(err => ({ err }))
}